var express = require('express');
var superagent = require('superagent');

var router = express.Router();

// 三方登录*******************************************************
router.post('/ajax/wxlogin', function(req, res, next) {
  console.log('三方登录');
  superagent
    .post(global.baseURL + '/api/users/social/login')
    .send({
      openId: req.body.openId,
      unionId: req.body.unionId
    })
    .set('DEVICE', 'weixin')
    .end(function(err1, res1) {
      if (res1 != null && res1.ok) {
        console.log('wechat_login:'+JSON.stringify(res1.body));
        res.send(res1.body);
      }
      else {
        console.error('请求数据错误! ');
        logger.error('请求数据错误。');
      }
    });
});

// 三方注册*******************************************************
router.post('/ajax/wxregister', function(req, res, next) {
  console.log('三方注册');
  superagent
    .post(global.baseURL + '/api/users/social/register')
    .send({
      nickName: req.body.nickName,
      avatar: req.body.avatar,
      sex: req.body.sex,
      openId: req.body.openId,
      unionId: req.body.unionId
    })
    .set('DEVICE', 'weixin')
    .end(function(err1, res1) {
      if (res1 !== undefined && res1.ok) {
        res.send(res1.body);
      }
      else {
        console.error('请求数据错误! ');
        logger.error('请求数据错误。');
      }
    });
});



module.exports = router;
